// pages/mycards/addcard/addcard.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgurl:app.globalData.imgurl,
    sendout: 0, //验证码发送状态
    sendCounts: 0,
    updateList: {
      idcard: {
        // 个人风采活动图片
        showVideo: false,
      }
    },
    services: [{
      text: '去火星散步',
      selected: 0,
    }, {
      text: '去火星散步',
      selected: 0,
    }, {
      text: '去火星散步',
      selected: 0,
    }, {
      text: '去火星散步',
      selected: 0,
    }, {
      text: '去火星散步',
      selected: 0,
    }, {
      text: '去火星散步',
      selected: 0,
    }],
    cardinfo: {
      selectedService:[],
      remark:''
    }
  },
  // 上传
  update: function (e) {
    var that = this
    var o = e.currentTarget.dataset;
    wx.chooseImage({
      sourceType: ['album', 'camera'],
      compressed: 'true',
      maxDuration: 10,
      success: function (res) {
        var imgurl = res.tempFilePaths[0]
        that.upload(o, imgurl)
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  // 上传
  upload: function (o, imgurl) {
    var that = this
    var updateList = that.data.updateList
    wx.uploadFile({
      url: app.globalData.url + 'Organize/upload',
      filePath: imgurl,
      name: 'image',
      success: function (res) {
        if (!res.data.code) {
          var data = JSON.parse(res.data)
          if (data.code == 1) {
            updateList[o.uptype].showImg = true
            updateList[o.uptype].imgUrl = data.path
            that.setData({
              updateList: updateList
            })
          } else {

          }
        }


      }
    })
  },
  // 获取姓名
  getName: function (e) {
    var cardinfo = this.data.cardinfo
    cardinfo.name = e.detail.value
    this.setData({
      cardinfo: cardinfo
    })
  },
  // 获取简介
  getDes: function (e) {
    var cardinfo = this.data.cardinfo
    cardinfo.des = e.detail.value
    this.setData({
      cardinfo: cardinfo
    })
  },
  // 获取工作信息
  getJobinfo: function (e) {
    var cardinfo = this.data.cardinfo
    cardinfo.jobinfo = e.detail.value
    this.setData({
      cardinfo: cardinfo
    })
  },
  // 选择服务亮点
  selectService: function (e) {
    var o = e.currentTarget.dataset
    var cardinfo = this.data.cardinfo
    var services = this.data.services
    services[o.index].selected = !services[o.index].selected
    this.setData({
      services:services
    })
  },
  // 获取手机号
  getPhone: function (e) {
    var cardinfo = this.data.cardinfo
    cardinfo.phone = e.detail.value
    this.setData({
      cardinfo: cardinfo
    })
  },
  // 发送验证码
  send: function (e) {
    var that = this
    var timer
    var sendout = 1
    var sendCounts = 10
    var phonenum = this.data.cardinfo.phone
    // 验证手机号 为true ,才发送验证码
    if (app.testPhone(phonenum)) {
      that.setData({
        sendout: sendout,
      })
      wx.request({
        url: app.globalData.url + 'Organize/sendsms?phone=' + phonenum,
        success: function (res) {
          console.log(res)
          that.setData({
            sendCounts: sendCounts
          })
          timer = setInterval(function () {
            sendCounts--;
            if (sendCounts == 0) {
              clearInterval(timer)
              sendout = 0
            }
            that.setData({
              sendCounts: sendCounts,
              sendout: sendout,
            })
          }, 1000)
        }
      })

    }

  },
  // 获取验证码
  getQrcode: function (e) {
    var cardinfo = this.data.cardinfo
    cardinfo.qrcode = e.detail.value
    this.setData({
      cardinfo: cardinfo
    })
  },
  // 获取邮箱
  getEmail: function (e) {
    var cardinfo = this.data.cardinfo
    cardinfo.email = e.detail.value
    this.setData({
      cardinfo: cardinfo
    })
  },
  // 获取地址
  getAddr: function (e) {
    var cardinfo = this.data.cardinfo
    cardinfo.address = e.detail.value
    this.setData({
      cardinfo: cardinfo
    })
  },
  // 获取关于我
  getRemark: function (e) {
    var cardinfo = this.data.cardinfo
    cardinfo.remark = e.detail.value
    this.setData({
      cardinfo: cardinfo
    })
  },
  // 获取打赏金额
  getMoney: function (e) {
    var cardinfo = this.data.cardinfo
    cardinfo.money = e.detail.value
    this.setData({
      cardinfo: cardinfo
    })
  },
  summit: function () {
    var cardinfo = this.data.cardinfo
    var services = this.data.services
    cardinfo.selectedService = []
    var updateList = this.data.updateList
    for (var i = 0; i < services.length; i++){
      if (services[i].selected == 1){
        cardinfo.selectedService.push(services[i].name)
      }
    }
    // if (!updateList.idcard.imgUrl || updateList.idcard.imgUrl ==''){
    //   this.showmodal('活动图片不能为空')
    // } else if (!cardinfo.des || cardinfo.des == '') {
       //   this.showmodal('姓名不能为空')
    // } else if (!cardinfo.des || cardinfo.des == '') {
    //   this.showmodal('简介不能为空')
    // } else if (!cardinfo.jobinfo || cardinfo.jobinfo == '') {
    //   this.showmodal('工作信息不能为空')
    // } else if (!cardinfo.selectedService || cardinfo.selectedService.length == 0) {
    //   this.showmodal('请选择亮点/服务')
    // } else if (!cardinfo.remark || cardinfo.remark == '') {
    //   this.showmodal('关于我不能为空')
    // } else if (!cardinfo.money || cardinfo.money == '') {
    //   this.showmodal('打赏金额不能为空')
    // } else {

      console.log(cardinfo)
      wx.request({
        url: app.globalData.url+ 'Card/addcard',
        data: {
          username: cardinfo.name ,
          wxopenid:wx.getStorageSync('openId'),
          description: cardinfo.des,
          head_pic: updateList.idcard.imgUrl,
          phone: cardinfo.phone,
          email: cardinfo.email ,
          address: cardinfo.address,
          content: cardinfo.remark,
          word: cardinfo.jobinfo,
          service: cardinfo.selectedService.join(','),
          award_money: cardinfo.money
        },
        success: function(res) {
          wx.navigateBack({
            delta: 1,
          })
        },
        fail: function(res) {},
        complete: function(res) {},
      })
    // }
  },
  showmodal:function(es){
    wx.showModal({
      title: '提示',
      content: es,
      confirmColor: '#ff8e03',
      showCancel: false,
    })
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.request({
      url: app.globalData.url + 'Card/cardtags',
      success: function (res) {
        console.log(res)
        var services = res.data.cardtags
        that.setData({
          services: services
        })
        
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})